﻿using Locker.FrameWork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Locker
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private PasswordProtectionForm _passwordForm;
        public MainWindow()
        {
            InitializeComponent();
        }
        public string SetErrorMessage
        {
            set
            {
                lblError.Content = value;
                lblError.Visibility = Visibility.Visible;
            }
        }
        private void Login_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (txtUserName.Text != "")
                {
                    if (txtPasswordBox.Password != "")
                    {
                        try
                        {
                            LoginDetails loginCred = new LoginDetails() { UserNameOrEmail = txtUserName.Text, PasswordHash = txtPasswordBox.Password };
                            Task<bool> taskCheckLoginCredetials = Task.Factory.StartNew(() => ActivatedUsers.AllowLogin(loginCred));
                            taskCheckLoginCredetials.ContinueWith((isSuccess) =>
                            {
                                if (isSuccess.Result)
                                {
                                    _passwordForm = new PasswordProtectionForm(loginCred);
                                    this.Close();
                                    _passwordForm.Show();
                                }
                                else
                                    SetErrorMessage = "Invalid Password or User Name/Email";
                            }, TaskScheduler.FromCurrentSynchronizationContext());
                        }
                        catch
                        {
                            SetErrorMessage = "Please Try again.";
                        }
                    }
                    else
                        SetErrorMessage = "Please enter Password";
                }
                else
                    SetErrorMessage = "Please Enter User Name";
            }
            catch
            {
                SetErrorMessage = "Please Try Again.";
            }
        }
    }
}
